<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Filter Acces Rightes
//-----------------------------------------------------------------

if(isset($is_guest))
{ header("location: $PHP_SELF?i=2"); exit; }

//-----------------------------------------------------------------
//  Make the function used to show the inbox statistics
//-----------------------------------------------------------------

function view_inbox_statistics()
{
	global $db, $template, $language, $config, $security_session;

	$messages_count = $db->getInfo_countRows("pm","r_user_id = '".$db->escape($_SESSION["$security_session"]['id'])."' AND r_user_user = '".$db->escape($_SESSION["$security_session"]['username'])."' AND r_delete = 1");
	$messages_limit = $config['inbox_msg'];
	$percent = round(($messages_count / $messages_limit) * 100, 1);

	$output = "<table width='100%' class='main' cellspacing='1' cellpadding='4'><tr><td class='con3'><table width='100%' cellspacing='0' cellpadding='0'><tr><td class='empty' width='99%'>{$language['PM Options desc4']} $percent% {$language['PM Options desc6']}</td><td class='empty' width='1%'>[$messages_count/$messages_limit]</td></tr></table></td></tr><tr><td class='con3'><table width='100%' cellpadding='0' cellspacing='0'><tr><td width='$percent%'><table width='100%' cellpadding='0' cellspacing='0'><tr><td width='2'><img src='templates/$template/images/progress/a.gif' width='2' height='16'></td><td width='100%' background='templates/$template/images/progress/b.gif'></td><td width='2'><img src='templates/$template/images/progress/c.gif' width='2' height='16'></td></tr></table></td><td width='100%'></td></tr></table></td></tr></table>";
	
return $output;
}

//-----------------------------------------------------------------
//  Make the function used to show the sentbox statistics
//-----------------------------------------------------------------

function view_sentbox_statistics()
{
	global $db, $template, $language, $config, $security_session;

	$messages_count = $db->getInfo_countRows("pm","s_user_id = '".$db->escape($_SESSION["$security_session"]['id'])."' AND s_user_user = '".$db->escape($_SESSION["$security_session"]['username'])."' AND s_delete = 1 AND type != 2");
	$messages_limit = $config['sentbox_msg'];
	$percent = round(($messages_count / $messages_limit) * 100, 1);

	$output = "<table width='100%' class='main' cellspacing='1' cellpadding='4'><tr><td class='con3'><table width='100%' cellspacing='0' cellpadding='0'><tr><td class='empty' width='99%'>{$language['PM Options desc5']} $percent% {$language['PM Options desc6']}</td><td class='empty' width='1%'>[$messages_count/$messages_limit]</td></tr></table></td></tr><tr><td class='con3'><table width='100%' cellpadding='0' cellspacing='0'><tr><td width='$percent%'><table width='100%' cellpadding='0' cellspacing='0'><tr><td width='2'><img src='templates/$template/images/progress/a.gif' width='2' height='16'></td><td width='100%' background='templates/$template/images/progress/b.gif'></td><td width='2'><img src='templates/$template/images/progress/c.gif' width='2' height='16'></td></tr></table></td><td width='100%'></td></tr></table></td></tr></table>";
	
return $output;
}

//-----------------------------------------------------------------
//  Start the switch
//-----------------------------------------------------------------

switch($a)
{
	//-----------------------------------------------------------------
	//  View Sentbox
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Navigation Stuff
		//-----------------------------------------------------------------

		$rows_per_page = $config['nav_pm'];
		$numrows = $db->getInfo_countRows("pm","s_user_id = '".$db->escape($_SESSION["$security_session"]['id'])."' AND s_user_user = '".$db->escape($_SESSION["$security_session"]['username'])."' AND s_delete = 1 AND type != 2");

		$lastpage = ceil($numrows/$rows_per_page);
		$p = (int)$p;
			
		if ($p < 1) $p = 1;
		elseif ($p > $lastpage) $p = $lastpage;

		if($numrows != FALSE) $limit = 'LIMIT ' .($p - 1) * $rows_per_page .',' .$rows_per_page;
		else $limit = NULL;

		//-----------------------------------------------------------------
		//  Sorting Stuff
		//-----------------------------------------------------------------

		if(!isset($s)) $s = 'r_date'; // Default value
		if(!isset($t)) $t = 'DESC'; // Default value

		$sname1 = "r_date";
		$sname2 = "subject";
		$sname3 = "s_date";
		$sname4 = "r_user_user";

		if($t == "ASC"){ $t = "DESC"; $t2 = "ASC"; }
		else{ $t = "ASC"; $t2 = "DESC"; }

		if($t == "ASC"){ $t3 = "<img src='templates/$template/images/misc/up.gif' border='0'>";}
		else{ $t3 = "<img src='templates/$template/images/misc/down.gif' border='0'>";}

		if($s == 1) $s = $sname1;
		if($s == 2) $s = $sname2;
		if($s == 3) $s = $sname3;
		if($s == 4) $s = $sname4;

		//-----------------------------------------------------------------
		//  Load pm table
		//-----------------------------------------------------------------

		$messages = $db->getTable("SELECT p.id AS pid, p.r_date, p.r_user_user, p.subject, p.s_date, p.r_user_id, u.id AS uid FROM pm AS p LEFT JOIN users AS u ON p.r_user_id=u.id WHERE p.s_user_id = '".$db->escape($_SESSION["$security_session"]['id'])."' AND p.s_user_user = '".$db->escape($_SESSION["$security_session"]['username'])."' AND p.s_delete = 1  AND type != 2 ORDER BY ".$db->escape($s)." ".$db->escape($t2)." ".$db->escape($limit)."");

		//-----------------------------------------------------------------
		//  Make a foreach to separate the results
		//-----------------------------------------------------------------

		if($messages != FALSE)
		{
			foreach($messages as $idx=>$message)
			{
				//-----------------------------------------------------------------
				//  Figure out if it's new or old
				//-----------------------------------------------------------------

				if($message['r_date'] == "unread") $new_old = "new";
				else $new_old = "old";

				//-----------------------------------------------------------------
				//  See if user exists
				//-----------------------------------------------------------------

				if(isset($message['uid'])) $message['r_user_user'] = "<a href='$PHP_SELF?n=modules/users&a=1&d={$message['r_user_id']}'>{$message['r_user_user']}</a>";
				else $message['r_user_user'] = $message['r_user_user'];

				//-----------------------------------------------------------------
				//  Make Layout
				//-----------------------------------------------------------------

				$layout .= "
					<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."' onclick=window.location.href='$PHP_SELF?n=modules/pm&a=3&d={$message['pid']}&s=$s&t=$t2&p=$p'>
					<td><img src='templates/$template/images/buttons/$new_old.gif' border='0' alt=''></td>
					<td><a href='$PHP_SELF?n=modules/pm&a=3&d={$message['pid']}&s=$s&t=$t2&p=$p'>{$message['subject']}</a></td>
					<td>".today_yesterday($message['s_date'])."</td>
					<td>{$message['r_user_user']}</td>
					<td><a href='$PHP_SELF?n=modules/pm&a=6&d={$message['pid']}&s=$s&t=$t2&p=$p'><img src='templates/$template/images/buttons/delete.gif' border='0' alt='{$language['PM Sentbox desc']}'></a></td>
					</tr>";

				//-----------------------------------------------------------------
				//  Start from 1
				//-----------------------------------------------------------------

				$idx++;
			}
		}
		else
		{
			//-----------------------------------------------------------------
			//  Reset to 0
			//-----------------------------------------------------------------

			$idx = 0;

			//-----------------------------------------------------------------
			//  Make Layout
			//-----------------------------------------------------------------

			$layout .= "<tr class='con1'><td colspan='5'>{$language['PM Sentbox desc2']}</td></tr>";
		}

		//-----------------------------------------------------------------
		//  Navigation Code
		//-----------------------------------------------------------------

		if($p == 1) $nav_prev = "<tr><td class='con1' align='right'>&nbsp;</td>";
		else
		{
			$prevpage = $p-1;
			$nav_prev = "<tr><td class='con1' align='left'><a href='$PHP_SELF?n=modules/pm&a=1&s=$s&t=$t2&p=$prevpage'>&laquo; {$language['PM Nav previous']}</a></td>";
		}

		$nav_nr = '[ '.paginate($lastpage, $p, $PHP_SELF.'?n=modules/pm&a=1&s='.$s.'&t='.$t2).' ]';

		if($idx == $numrows) $nav_nr = NULL;

		if($p == $lastpage) $nav_next = "<td class='con1' align='right'>&nbsp;</td></tr>";
		else
		{
			$nextcode = $numrows - $idx * $p;
			if($nextcode > $idx) $how_next = $idx;
			else $how_next = $nextcode;

			$nextpage = $p+1;

			$nav_next = "<td class='con1' align='right'><a href='$PHP_SELF?n=modules/pm&a=1&s=$s&t=$t2&p=$nextpage'>{$language['PM Nav next']} $how_next &raquo;</a></td></tr>";
		}

		if($idx == $numrows)
		{
			$nav_head = "class='con1'";
			$nav = NULL;
		}
		else
		{
			$nav_head = "class='head'";
			$nav = ''.$nav_prev.''.$nav_next.'';
		}

		//-----------------------------------------------------------------
		//  Start Layout + Navigation
		//-----------------------------------------------------------------

		?>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='head'><td colspan='5'><?= $language['PM Options name'] ?></td></tr>
			<tr class="bottom">
			<td width="50%"><?= $language['PM Options title'] ?></td>
			<td width="50%"><?= $language['PM Options title2'] ?></td>
			</tr>
			<tr class="con1">
			<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm'><a href="<?= $PHP_SELF ?>?n=modules/pm"><?= $language['PM Options desc'] ?></a></td>
			<td width="50%" rowspan="3"><?= view_sentbox_statistics(); ?></td>
			</tr>
			<tr class="con1">
			<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm&a=1'><a href="<?= $PHP_SELF ?>?n=modules/pm&a=1"><?= $language['PM Options desc2'] ?></a></td>
			</tr>
			<tr class="con1">
			<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm&a=4'><a href="<?= $PHP_SELF ?>?n=modules/pm&a=4"><?= $language['PM Options desc3'] ?></a></td>
			</tr>
			</table>
			<br>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='head'><td colspan='5'><?= $language['PM Sentbox name'] ?></td></tr>
			<tr class="bottom">
			<td width="1%" align="center"><a href="<?= $PHP_SELF ?>?n=modules/pm&a=1&s=1&t=<?= $t ?>&p=<?= $p ?>"><? if($s == $sname1) echo $t3; else echo "*" ?></a></td>
			<td width="50%"><a href="<?= $PHP_SELF ?>?n=modules/pm&a=1&s=2&t=<?= $t ?>&p=<?= $p ?>"><?= $language['PM Sentbox title'] ?> <? if($s == $sname2) echo $t3 ?></a></td>
			<td width="30%"><a href="<?= $PHP_SELF ?>?n=modules/pm&a=1&s=3&t=<?= $t ?>&p=<?= $p ?>"><?= $language['PM Sentbox title2'] ?> <? if($s == $sname3) echo $t3 ?></a></td>
			<td width="18%"><a href="<?= $PHP_SELF ?>?n=modules/pm&a=1&s=4&t=<?= $t ?>&p=<?= $p ?>"><?= $language['PM Sentbox title3'] ?> <? if($s == $sname4) echo $t3 ?></a></td>
			<td width="1%">&nbsp;</td>
			</tr>
			<?= $layout ?>
			</table>
			<br>
			<table class="main" cellspacing="1" cellpadding="4" width="100%">
			<col width="50%"><col width="50%">
			<tr <?= $nav_head ?>>
			<td colspan="2">
			<table border="0" cellspacing="0" cellpadding="0" width="100%">
			<col width="50%"><col width="50%">
			<tr class="empty">
			<td align="left"><?= $language['PM Nav nav'] ?> <b><?= $idx ?></b> <?= $language['PM Nav nav2'] ?> <b><?= $numrows ?></b></td>
			<td align="right"><?= $nav_nr ?></td>
			</tr>
			</table>
			</td>
			</tr>
			<?= $nav ?>
			</table>
			<br>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr><td class="con1" width="50%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="7%" class="empty"><img src="templates/<?= $template ?>/images/buttons/new.gif" border="0"></td><td width="93%" class="empty"><?= $language['PM Flag desc3'] ?></td></tr>
			</table>
			</td><td class="con1" width="50%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="7%" class="empty"><img src="templates/<?= $template ?>/images/buttons/old.gif" border="0"></td><td width="93%" class="empty"><?= $language['PM Flag desc4'] ?></td></tr>
			</table>
			</td></tr>
			</table>
		<?

	break;

	//-----------------------------------------------------------------
	//  View Inbox Message
	//-----------------------------------------------------------------

	case 2:

		//-----------------------------------------------------------------
		//  Read Message + Check for existance + User Information
		//-----------------------------------------------------------------

		$message = $db->getLine("SELECT p.id AS pid, p.r_date, p.subject, p.s_user_user, p.r_user_id, p.r_user_user, p.s_date, p.message, p.s_user_id, u.id AS uid, u.hideemail, u.email, u.avatar, u.authorization, u.url, u.msn, u.yahoo, u.icq, u.aim, u.skype, u.signature, o.user_id AS oid FROM pm AS p LEFT JOIN users AS u ON u.id=p.s_user_id LEFT JOIN online AS o ON (o.user_id=u.id AND o.type='live') WHERE p.id = '".$db->escape($d)."' AND p.r_user_id = '".$db->escape($_SESSION["$security_session"]['id'])."' AND p.r_user_user = '".$db->escape($_SESSION["$security_session"]['username'])."'");
		if(!$message) header("location: $PHP_SELF?i=error");

		//-----------------------------------------------------------------
		//  Check if user is online
		//-----------------------------------------------------------------

		if(isset($message['oid'])) $status = "<td width='21%'><img src='templates/$template/images/buttons/on.gif' alt='{$language['PM View desc2']}'></td>";
		else $status = "<td width='24%'><img src='templates/$template/images/buttons/off.gif' alt='{$language['PM View desc3']}'></td>";

		//-----------------------------------------------------------------
		//  Check if user exists
		//-----------------------------------------------------------------

		if(!isset($message['uid'])) $message['hideemail'] = 1;
		else $message['s_user_user'] = "<a href='$PHP_SELF?n=modules/users&a=1&d={$message['s_user_id']}'>{$message['s_user_user']}</a>";

		//-----------------------------------------------------------------
		//  Condition for all the buttons
		//-----------------------------------------------------------------

		if($message['hideemail'] != "1") $user_email = "<td width='1%'><a href='mailto:{$message['email']}'><img src='templates/$template/images/buttons/email.gif' border='0'></a></td><td width='3'></td>"; else $user_email = NULL;
		if(isset($message['url'])) $user_website = "<td width='1%'><a href='http://{$message['url']}' target='_blank'><img src='templates/$template/images/buttons/www.gif' border='0'></a></td><td width='3'></td>"; else $user_website = NULL;
		if(isset($message['msn'])) $user_msn = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$message['uid']}'><img src='templates/$template/images/buttons/msn.gif' border='0'></a></td><td width='3'></td>"; else $user_msn = NULL;
		if(isset($message['yahoo'])) $user_yahoo = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$message['uid']}'><img src='templates/$template/images/buttons/yahoo.gif' border='0'></a></td><td width='3'></td>"; else $user_yahoo = NULL;
		if(isset($message['icq'])) $user_icq = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$message['uid']}'><img src='templates/$template/images/buttons/icq.gif' border='0'></a></td><td width='3'></td>"; else $user_icq = NULL;
		if(isset($message['aim'])) $user_aim = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$message['uid']}'><img src='templates/$template/images/buttons/aim.gif' border='0'></a></td><td width='3'></td>"; else $user_aim = NULL;
		if(isset($message['skype'])) $user_skype = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$message['uid']}'><img src='templates/$template/images/buttons/skype.gif' border='0'></a></td><td width='3'></td>"; else $user_skype = NULL;
		if(isset($message['signature'])) $user_signature = "<br><br><br>--------------------<br>".parse_message($message['signature']); else $user_signature = NULL;

		//-----------------------------------------------------------------
		//  Make it Read and Old
		//-----------------------------------------------------------------

		if($message['r_date'] == "unread")
		{
			$db->updateRow("pm",array('r_date' => ''.time().''),"id = ".$db->escape($d)."");
			$message_read = $language['PM View desc6'];
		}
		else $message_read = today_yesterday($message['r_date']);

		//-----------------------------------------------------------------
		//  Start Layout
		//-----------------------------------------------------------------

		?>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='head'><td colspan='5'><?= $language['PM Options name'] ?></td></tr>
			<tr class="bottom">
			<td width="50%"><?= $language['PM Options title'] ?></td>
			<td width="50%"><?= $language['PM Options title2'] ?></td>
			</tr>
			<tr class="con1">
			<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm'><a href="<?= $PHP_SELF ?>?n=modules/pm"><?= $language['PM Options desc'] ?></a></td>
			<td width="50%" rowspan="3"><?= view_inbox_statistics(); ?></td>
			</tr>
			<tr class="con1">
			<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm&a=1'><a href="<?= $PHP_SELF ?>?n=modules/pm&a=1"><?= $language['PM Options desc2'] ?></a></td>
			</tr>
			<tr class="con1">
			<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm&a=4'><a href="<?= $PHP_SELF ?>?n=modules/pm&a=4"><?= $language['PM Options desc3'] ?></a></td>
			</tr>
			</table>
			<br>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='head'><td colspan='5'><img src='templates/<?= $template ?>/images/misc/nav.gif' border='0'> <?= $message['subject'] ?></td></tr>
			<tr class="bottom">
			<td width="25%"><span style="font-size: 12px; font-weight: bold;"><?= $message['s_user_user'] ?></span></td>
			<td width="75%" colspan='2'><table width='100%' cellspacing='0' cellpadding='0'><tr class='empty'><td width='80%' align='left'><b><font color='#848484'><img src='templates/<?= $template ?>/images/buttons/date.gif' border='0' alt='<?= $language['PM View desc4'] ?>'> <?= today_yesterday($message['s_date']) ?></font></b></td><td width='20%' align='right'><span class='inv'>#<?= $message['pid'] ?></span></td></tr></table></td>
			</tr>
			<tr class="con1">
			<td valign="top"><? if(isset($message['uid'])){ ?><table border='0' cellspacing='0' cellpadding='0'><tr class='empty'><td width='79' height='80' class='avatar'><?= find_avatar($message['avatar']) ?></td></tr></table><?= find_authorization($message['authorization']); } else echo $language['PM View desc11']; ?><br><br><?= $language['PM View desc5'] ?><br><?= $message_read ?></td>
			<td valign="top"><?= parse_message($message['message']); echo $user_signature ?></td>
			<td width='1%' class='con2' valign='top'><? if(isset($message['uid'])){ ?><a href="<?= $PHP_SELF ?>?n=modules/pm&a=4&d=<?= $message['s_user_id'] ?>&o=<?= $message['pid'] ?>"><img src='templates/<?= $template ?>/images/buttons/reply.gif' border='0' alt='<?= $language['PM View desc8'] ?>'></a><table><tr><td></td></tr></table><? } ?><a href="<?= $PHP_SELF ?>?n=modules/pm&a=5&d=<?= $message['pid'] ?>&s=<?= $s ?>&t=<?= $t ?>&p=<?= $p ?>"><img src='templates/<?= $template ?>/images/buttons/delete2.gif' border='0' alt='<?= $language['PM View desc10'] ?>'></a></td>
			</tr>
			<tr class='con2'><td><table cellspacing='0' cellpadding='0' width='100%'><tr class='empty'><? if(isset($message['uid'])){ ?><?= $status ?><td><a href='<?= $PHP_SELF ?>?n=modules/users&a=1&d=<?= $message['uid'] ?>'><img src='templates/<?= $template ?>/images/buttons/profile.gif' border='0'></a></td><td align='right'><a href='<?= $PHP_SELF ?>?n=modules/pm&a=4&d=<?= $message['uid'] ?>'><img src='templates/<?= $template ?>/images/buttons/pm.gif' border='0'></a></td><? } ?></tr></table></td><td colspan='2'><table border='0' cellspacing='0' cellpadding='0' width='100%'><tr class='empty'><?= "$user_email$user_website$user_msn$user_yahoo$user_icq$user_aim$user_skype" ?><td align='right'><a href="<?= $PHP_SELF ?>?n=modules/pm&a=4&q=<?= $message['pid'] ?>"><img src='templates/<?= $template ?>/images/buttons/quote.gif' border='0' alt='<?= $language['PM View desc9'] ?>'></a></td></tr></table></td></tr>
			<tr class='con4'><td colspan='3'></td></tr>
			</table>
		<?

	break;

	//-----------------------------------------------------------------
	//  View Sentbox Message
	//-----------------------------------------------------------------

	case 3:

		//-----------------------------------------------------------------
		//  Read Message + Check for existance + User Information
		//-----------------------------------------------------------------

		$message = $db->getLine("SELECT p.id AS pid, p.r_date, p.subject, p.s_user_user, p.s_date, p.message, p.s_user_id, u.id AS uid, u.hideemail, u.email, u.avatar, u.authorization, u.url, u.msn, u.yahoo, u.icq, u.aim, u.skype, u.signature, o.user_id AS oid FROM pm AS p LEFT JOIN users AS u ON u.id=p.s_user_id LEFT JOIN online AS o ON (o.user_id=u.id AND o.type='live') WHERE p.id = '".$db->escape($d)."' AND p.s_user_id = '".$db->escape($_SESSION["$security_session"]['id'])."' AND p.s_user_user = '".$db->escape($_SESSION["$security_session"]['username'])."'");
		if(!$message) header("location: $PHP_SELF?i=error");

		//-----------------------------------------------------------------
		//  Check if user is online
		//-----------------------------------------------------------------

		if(isset($message['oid'])) $status = "<td width='21%'><img src='templates/$template/images/buttons/on.gif' alt='{$language['PM View desc2']}'></td>";
		else $status = "<td width='24%'><img src='templates/$template/images/buttons/off.gif' alt='{$language['PM View desc3']}'></td>";

		//-----------------------------------------------------------------
		//  Condition for all the buttons
		//-----------------------------------------------------------------

		if($message['hideemail'] != "1") $user_email = "<td width='1%'><a href='mailto:{$message['email']}'><img src='templates/$template/images/buttons/email.gif' border='0'></a></td><td width='3'></td>"; else $user_email = NULL;
		if(isset($message['url'])) $user_website = "<td width='1%'><a href='http://{$message['url']}' target='_blank'><img src='templates/$template/images/buttons/www.gif' border='0'></a></td><td width='3'></td>"; else $user_website = NULL;
		if(isset($message['msn'])) $user_msn = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$message['uid']}'><img src='templates/$template/images/buttons/msn.gif' border='0'></a></td><td width='3'></td>"; else $user_msn = NULL;
		if(isset($message['yahoo'])) $user_yahoo = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$message['uid']}'><img src='templates/$template/images/buttons/yahoo.gif' border='0'></a></td><td width='3'></td>"; else $user_yahoo = NULL;
		if(isset($message['icq'])) $user_icq = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$message['uid']}'><img src='templates/$template/images/buttons/icq.gif' border='0'></a></td><td width='3'></td>"; else $user_icq = NULL;
		if(isset($message['aim'])) $user_aim = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$message['uid']}'><img src='templates/$template/images/buttons/aim.gif' border='0'></a></td><td width='3'></td>"; else $user_aim = NULL;
		if(isset($message['skype'])) $user_skype = "<td width='1%'><a href='$PHP_SELF?n=modules/users&a=1&d={$message['uid']}'><img src='templates/$template/images/buttons/skype.gif' border='0'></a></td><td width='3'></td>"; else $user_skype = NULL;
		if(isset($message['signature'])) $user_signature = "<br><br><br>--------------------<br>".parse_message($message['signature']); else $user_signature = NULL;

		//-----------------------------------------------------------------
		//  Message Read
		//-----------------------------------------------------------------

		if($message['r_date'] != "unread") $message_read = today_yesterday($message['r_date']);
		else $message_read = $language['PM View desc7'];

		//-----------------------------------------------------------------
		//  Start Layout
		//-----------------------------------------------------------------

		?>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='head'><td colspan='5'><?= $language['PM Options name'] ?></td></tr>
			<tr class="bottom">
			<td width="50%"><?= $language['PM Options title'] ?></td>
			<td width="50%"><?= $language['PM Options title2'] ?></td>
			</tr>
			<tr class="con1">
			<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm'><a href="<?= $PHP_SELF ?>?n=modules/pm"><?= $language['PM Options desc'] ?></a></td>
			<td width="50%" rowspan="3"><?= view_sentbox_statistics(); ?></td>
			</tr>
			<tr class="con1">
			<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm&a=1'><a href="<?= $PHP_SELF ?>?n=modules/pm&a=1"><?= $language['PM Options desc2'] ?></a></td>
			</tr>
			<tr class="con1">
			<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm&a=4'><a href="<?= $PHP_SELF ?>?n=modules/pm&a=4"><?= $language['PM Options desc3'] ?></a></td>
			</tr>
			</table>
			<br>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='head'><td colspan='5'><img src='templates/<?= $template ?>/images/misc/nav.gif' border='0'> <?= $message['subject'] ?></td></tr>
			<tr class="bottom">
			<td width="25%"><span style="font-size: 12px; font-weight: bold;"><a href="<?= $PHP_SELF ?>?n=modules/users&a=1&d=<?= $message['s_user_id'] ?>"><?= $message['s_user_user'] ?></a></span></td>
			<td width="75%" colspan="2"><table width='100%' cellspacing='0' cellpadding='0'><tr class='empty'><td width='80%' align='left'><b><font color='#848484'><img src='templates/<?= $template ?>/images/buttons/date.gif' border='0' alt='<?= $language['PM View desc4'] ?>'> <?= today_yesterday($message['s_date']) ?></font></b></td><td width='20%' align='right'><span class='inv'>#<?= $message['pid'] ?></span></td></tr></table></td>
			</tr>
			<tr>
			<td class='con1' valign="top"><table border='0' cellspacing='0' cellpadding='0'><tr class='empty'><td width='79' height='80' class='avatar'><?= find_avatar($message['avatar']) ?></td></tr></table><?= find_authorization($message['authorization']) ?><br><br><?= $language['PM View desc5'] ?><br><?= $message_read ?></td>
			<td class='con1' valign="top"><?= parse_message($message['message']); echo $user_signature ?></td>
			<td width='1%' class='con2' valign='top'><a href="<?= $PHP_SELF ?>?n=modules/pm&a=6&d=<?= $message['pid'] ?>&s=<?= $s ?>&t=<?= $t ?>&p=<?= $p ?>"><img src='templates/<?= $template ?>/images/buttons/delete2.gif' border='0' alt='<?= $language['PM View desc10'] ?>'></a></td>
			</tr>
			<tr class='con2'><td><table cellspacing='0' cellpadding='0' width='100%'><tr class='empty'><?= $status ?><td><a href='<?= $PHP_SELF ?>?n=modules/users&a=1&d=<?= $message['uid'] ?>'><img src='templates/<?= $template ?>/images/buttons/profile.gif' border='0'></a></td><td align='right'><a href='<?= $PHP_SELF ?>?n=modules/pm&a=4&d=<?= $message['uid'] ?>'><img src='templates/<?= $template ?>/images/buttons/pm.gif' border='0'></a></td></tr></table></td><td colspan="2"><table border='0' cellspacing='0' cellpadding='0' width='100%'><tr class='empty'><?= "$user_email$user_website$user_msn$user_yahoo$user_icq$user_aim$user_skype" ?><td align='right'><a href="<?= $PHP_SELF ?>?n=modules/pm&a=4&q=<?= $message['pid'] ?>"><img src='templates/<?= $template ?>/images/buttons/quote.gif' border='0' alt='<?= $language['PM View desc9'] ?>'></a></td></tr></table></td></tr>
			<tr class='con4'><td colspan="3"></td></tr>
			</table>
		<?

	break;

	//-----------------------------------------------------------------
	//  Compose Message
	//-----------------------------------------------------------------

	case 4:

		switch($c)
		{
			//-----------------------------------------------------------------
			//  Send Private Message
			//-----------------------------------------------------------------

			case 1:

				//-----------------------------------------------------------------
				//  Check if you'r sentbox is not full
				//-----------------------------------------------------------------

				$messages_count = $db->getInfo_countRows("pm","s_user_id = '{$_SESSION["$security_session"]['id']}' AND s_user_user = '{$_SESSION["$security_session"]['username']}' AND s_delete = 1 AND type != 2");
				$messages_limit = $config['sentbox_msg'];

				if($messages_count >= $messages_limit){ header("location: $PHP_SELF?n=modules/pm&a=4&i=47"); exit; }

				//-----------------------------------------------------------------
				//  Check if his inbox is not full
				//-----------------------------------------------------------------

				$messages_count = $db->getInfo_countRows("pm","r_user_user = '".$db->escape($senduser)."' AND r_delete = 1");
				$messages_limit = $config['inbox_msg'];

				if($messages_count >= $messages_limit){ header("location: $PHP_SELF?n=modules/pm&a=4&i=48"); exit; }

				//-----------------------------------------------------------------
				//  Check if you have some empty fields
				//-----------------------------------------------------------------

				if(!$senduser && !isset($u)){ header("location: $PHP_SELF?n=modules/pm&a=4&i=49"); exit; }
				if(!$sendsubject){ header("location: $PHP_SELF?n=modules/pm&a=4".(isset($u)?"&u=1":NULL)."&i=50"); exit; }
				if(!$sendmessage){ header("location: $PHP_SELF?n=modules/pm&a=4".(isset($u)?"&u=1":NULL)."&i=51"); exit; }

				//-----------------------------------------------------------------
				//  BBcode check
				//-----------------------------------------------------------------

				for ($i = 1; $i <= 5; $i++) if(check_tag_order($sendmessage) == $i) { header("location: $PHP_SELF?n=modules/pm&a=4".(isset($u)?"&u=1":NULL)."&i=9$i"); exit; }

				//-----------------------------------------------------------------
				//  Take care of some things
				//-----------------------------------------------------------------

				$sendsubject = jcm_htmlspecialchars(trim(jcm_stripslashes(do_censor($sendsubject))));
				$sendmessage = jcm_linebreaks(trim(jcm_stripslashes($sendmessage)));
				if($overflow = check_tag_order($sendmessage)) $sendmessage = substr($sendmessage, 0, $overflow[0]).substr($sendmessage, $overflow[1], (strlen($sendmessage) - $overflow[0]));
				
				//-----------------------------------------------------------------
				//  Is It A Mass PM
				//-----------------------------------------------------------------

				if(isset($is_administrator) || isset($is_webmaster) && isset($u))
				{
					//-----------------------------------------------------------------
					//  Prepare Subject/Message
					//-----------------------------------------------------------------				
					
					$sendsubject = "{$config['mass_pm_subject']}{$sendsubject}";
					$sendmessage = "{$sendmessage}\n\n\n{$config['mass_pm_message']}";
				
					//-----------------------------------------------------------------
					//  Load Users
					//-----------------------------------------------------------------
					
					$users = $db->getTable("SELECT id, username FROM users");
					
					//-----------------------------------------------------------------
					//  Foreach Users and Send
					//-----------------------------------------------------------------
					
					foreach($users as $user)
					{
						//-----------------------------------------------------------------
						//  After all that checking I'm thirsty for some database store ;)
						//-----------------------------------------------------------------
		
						$pm_add['s_user_id'] = $_SESSION["$security_session"]['id'];
						$pm_add['s_user_user'] = $_SESSION["$security_session"]['username'];
						$pm_add['r_user_id'] = $user['id'];
						$pm_add['r_user_user'] = $user['username'];
						$pm_add['s_date'] = time();
						$pm_add['subject'] = $sendsubject;
						$pm_add['message'] = $sendmessage;
						$pm_add['type'] = 2;
		
						$db->insertRow("pm",$pm_add);
					}
					
					//-----------------------------------------------------------------
					//  Write Log
					//-----------------------------------------------------------------
	
					$db->insertRow("log",array('user_id' => ''.$db->escape($_SESSION["$security_session"]['id']).'','user_user' => ''.$db->escape($_SESSION["$security_session"]['username']).'','log' => 'Mass PM Sent -> '.$sendsubject.'','date' => ''.time().''));
				}
				else
				{
					//-----------------------------------------------------------------
					//  Check if the user you specified exists
					//-----------------------------------------------------------------
		
					$check_user = $db->getLine("SELECT id, username FROM users WHERE username = '".$db->escape($senduser)."'");
					if($check_user == FALSE){ header("location: $PHP_SELF?n=modules/pm&a=4&i=52"); exit; }		
		
					//-----------------------------------------------------------------
					//  After all that checking I'm thirsty for some database store ;)
					//-----------------------------------------------------------------
	
					$pm_add['s_user_id'] = $_SESSION["$security_session"]['id'];
					$pm_add['s_user_user'] = $_SESSION["$security_session"]['username'];
					$pm_add['r_user_id'] = $check_user['id'];
					$pm_add['r_user_user'] = $check_user['username'];
					$pm_add['s_date'] = time();
					$pm_add['subject'] = $sendsubject;
					$pm_add['message'] = $sendmessage;
					$pm_add['type'] = 1;
	
					$db->insertRow("pm",$pm_add);
				}

				//-----------------------------------------------------------------
				//  Redirect when finished
				//-----------------------------------------------------------------

				header("location: $PHP_SELF?n=modules/pm&a=1&i=".(!isset($u)?"53":"135")."");

			break;

			//-----------------------------------------------------------------
			//  Default case (View Send Form)
			//-----------------------------------------------------------------

			default:

				//-----------------------------------------------------------------
				//  Find User, Subject and Message for Reply/Quote + Trick
				//-----------------------------------------------------------------

				if(isset($d))
				{
					$users = $db->getLine("SELECT * FROM users WHERE id = '".$db->escape($d)."'");
					$reply_a = $users['username']; 
				}
				else $reply_a = NULL;

				if(isset($o))
				{
					$message = $db->getLine("SELECT * FROM pm WHERE id = '".$db->escape($o)."'");

					if(strpos($message['subject'], 'RE: ') !== false)
					{
						if(strpos($message['subject'], 'RE: ') !== false) $reply_b = "[2]RE: ".str_replace("RE: ","", $message['subject'])."";

						for($count_a = 1; $count_a < 100; $count_a++)
						{
							$count_b = $count_a + 1;
							if(strpos($message['subject'], "[$count_a]RE: ") !== false) $reply_b = "[$count_b]RE: ".str_replace("[$count_a]RE: ","", $message['subject'])."";
						}
					}
					else
						$reply_b = "RE: {$message['subject']}"; 
				}
				else $reply_b = NULL;

				if(isset($q))
				{
					$message = $db->getLine("SELECT * FROM pm WHERE id = '".$db->escape($q)."' AND s_user_id = '".$db->escape($_SESSION["$security_session"]['id'])."' OR id = '".$db->escape($q)."' AND r_user_id = '".$db->escape($_SESSION["$security_session"]['id'])."'");
					$reply_c = "[quote={$message['s_user_user']}]{$message['message']}[/quote]"; 
				}
				else $reply_c = NULL;

				//-----------------------------------------------------------------
				//  Start Layout
				//-----------------------------------------------------------------
			
				if(isset($is_administrator) || isset($is_webmaster) && isset($u))
				{
				?>
					<table class="main" cellspacing="1" cellpadding="4">
					<tr class='con1'><td><a href='<?= $PHP_SELF ?>?n=modules/admin'><?= $language['Admin name'] ?></a> &raquo; <?= $language['Admin site desc19'] ?></td></tr>
					</table>
					<table><tr><td></td></tr></table>				
				<?
				}
				?>
					<table class="main" cellspacing="1" cellpadding="4">
					<tr class='head'><td colspan='5'><?= $language['PM Options name'] ?></td></tr>
					<tr class="bottom">
					<td width="50%"><?= $language['PM Options title'] ?></td>
					<td width="50%"><?= $language['PM Options title2'] ?></td>
					</tr>
					<tr class="con1">
					<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm'><a href="<?= $PHP_SELF ?>?n=modules/pm"><?= $language['PM Options desc'] ?></a></td>
					<td width="50%" rowspan="3"><?= view_sentbox_statistics(); ?></td>
					</tr>
					<tr class="con1">
					<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm&a=1'><a href="<?= $PHP_SELF ?>?n=modules/pm&a=1"><?= $language['PM Options desc2'] ?></a></td>
					</tr>
					<tr class="con1">
					<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm&a=4'><a href="<?= $PHP_SELF ?>?n=modules/pm&a=4"><?= $language['PM Options desc3'] ?></a></td>
					</tr>
					</table>
					<br>
					<form action="<?= $PHP_SELF ?>?n=modules/pm<?= ($u == 1)?"&u=$u":"" ?>" method="post" name="textarea">
					<table class="main" cellspacing="1" cellpadding="4">
					<col width="25%"><col width="75%">
					<tr class='head'><td colspan='5'><?= $language['PM Compose name'] ?></td></tr>
					<? 	/* Dont Touch This Line*/ if(isset($is_administrator) || isset($is_webmaster)): ?>
						<tr class="con1">
						<td><?= $language['PM Compose desc'] ?></td>
						<td><?= (!isset($u))?"<input style='width:40%' maxlength='35' class='box' value='$reply_a' name='senduser'> | <a href='$PHP_SELF?n=modules/pm&a=4&u=1'>{$language['PM Compose desc9']}</a>":"{$language['PM Compose desc11']} | <a href='$PHP_SELF?n=modules/pm&a=4'>{$language['PM Compose desc10']}</a>" ?></td>
						</tr>
					<? /* Dont Touch This Line*/ else: ?>
						<tr class="con1">
						<td><?= $language['PM Compose desc'] ?></td>
						<td><input style='width:40%' maxlength='35' class='box' value='<?= $reply_a ?>' name='senduser'></td>
						</tr>
					<? /* Dont Touch This Line*/ endif; ?>
					<tr class="con1">
					<td><?= $language['PM Compose desc3'] ?></td>
					<td><input style="width:40%" maxlength="35" class="box" value="<?= $reply_b ?>" name="sendsubject"></td>
					</tr>
					<? if($config['emoticons'] == 1) { ?>
					<tr class="con1">
					<td><?= $language['PM Compose desc4'] ?></td>
					<td><?= insertSmilies(20) ?></td>
					</tr>
					<? } if($config['bbcode'] == 1) { ?>
					<tr class="con1">
					<td><?= $language['PM Compose desc5'] ?></td>
					<td><?= insertBBcode(20) ?></td>
					</tr>
					<? } ?>
					<tr class="con1">
					<td valign="top"><?= "{$language['PM Compose desc6']}".dis_bb_em(true)."" ?></td>
					<td><textarea style="width:100%; height:205px;" class="box" name="sendmessage"><?= $reply_c ?></textarea></td>
					</tr>
					<tr class="con1">
					<td><?= $language['PM Compose desc7'] ?></td>
					<td><input type="submit" style="width:120" class="box" value="<?= $language['PM Compose desc8'] ?>"></td>
					</tr>
					</table>
					<input type="hidden" name="a" value="4">
					<input type="hidden" name="c" value="1">
					</form>
				<?
			break;
		}
	break;

	//-----------------------------------------------------------------
	//  Delete Inbox Message
	//-----------------------------------------------------------------

	case 5:

		//-----------------------------------------------------------------
		//  Read Message
		//-----------------------------------------------------------------

		$message = $db->getLine("SELECT * FROM pm WHERE id = '".$db->escape($d)."' AND r_user_id = '".$db->escape($_SESSION["$security_session"]['id'])."' AND r_user_user = '".$db->escape($_SESSION["$security_session"]['username'])."'");
		if(!$message){ header("location: $PHP_SELF?i=error"); exit; }

		//-----------------------------------------------------------------
		//  Delete it
		//-----------------------------------------------------------------

		if($message['s_delete'] == 1 || $message['r_delete'] == 1) $db->deleteRow("pm","id = '".$db->escape($d)."'");
		else $db->updateRow("pm",array('r_delete' => '1'),"id = ".$db->escape($d)."");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/pm&s=$s&t=$t&p=$p&i=54");

	break;

	//-----------------------------------------------------------------
	//  Delete Sentbox Message
	//-----------------------------------------------------------------

	case 6:

		//-----------------------------------------------------------------
		//  Read Message
		//-----------------------------------------------------------------

		$message = $db->getLine("SELECT * FROM pm WHERE id = '".$db->escape($d)."' AND s_user_id = '".$db->escape($_SESSION["$security_session"]['id'])."' AND s_user_user = '".$db->escape($_SESSION["$security_session"]['username'])."'");
		if(!$message){ header("location: $PHP_SELF?i=error"); exit; }

		//-----------------------------------------------------------------
		//  Delete it
		//-----------------------------------------------------------------

		if($message['s_delete'] == 1 || $message['r_delete'] == 1) $db->deleteRow("pm","id = '".$db->escape($d)."'");
		else $db->updateRow("pm",array('s_delete' => '1'),"id = ".$db->escape($d)."");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/pm&a=1&s=$s&t=$t&p=$p&i=55");

	break;

	//-----------------------------------------------------------------
	//  Default case (Inbox)
	//-----------------------------------------------------------------

	default:

		//-----------------------------------------------------------------
		//  Navigation Stuff
		//-----------------------------------------------------------------

		$rows_per_page = $config['nav_pm'];
		$numrows = $db->getInfo_countRows("pm","r_user_id = '".$db->escape($_SESSION["$security_session"]['id'])."' AND r_user_user = '".$db->escape($_SESSION["$security_session"]['username'])."' AND r_delete = 1");

		$lastpage = ceil($numrows/$rows_per_page);
		$p = (int)$p;
		
		if ($p < 1) $p = 1;
		elseif ($p > $lastpage) $p = $lastpage;

		if($numrows != FALSE) $limit = 'LIMIT ' .($p - 1) * $rows_per_page .',' .$rows_per_page;
		else $limit = NULL;

		//-----------------------------------------------------------------
		//  Sorting Stuff
		//-----------------------------------------------------------------

		if(!isset($s)) $s = 'r_date'; // Default value
		if(!isset($t)) $t = 'DESC'; // Default value

		$sname1 = "r_date";
		$sname2 = "subject";
		$sname3 = "s_date";
		$sname4 = "user_user";

		if($t == "ASC"){ $t = "DESC"; $t2 = "ASC"; }
		else{ $t = "ASC"; $t2 = "DESC"; }

		if($t == "ASC"){ $t3 = "<img src='templates/$template/images/misc/up.gif' border='0'>";}
		else{ $t3 = "<img src='templates/$template/images/misc/down.gif' border='0'>";}

		if($s == 1) $s = $sname1;
		if($s == 2) $s = $sname2;
		if($s == 3) $s = $sname3;
		if($s == 4) $s = $sname4;

		//-----------------------------------------------------------------
		//  Load pm table
		//-----------------------------------------------------------------

		$messages = $db->getTable("SELECT p.id AS pid, p.s_user_id, p.r_date, p.s_user_user, p.subject, p.s_date, u.id AS uid FROM pm AS p LEFT JOIN users AS u ON p.s_user_id=u.id WHERE p.r_user_id = '".$db->escape($_SESSION["$security_session"]['id'])."' AND p.r_user_user = '".$db->escape($_SESSION["$security_session"]['username'])."' AND p.r_delete = 1 ORDER BY ".$db->escape($s)." ".$db->escape($t2)." ".$db->escape($limit)."");

		//-----------------------------------------------------------------
		//  Make a foreach to separate the results
		//-----------------------------------------------------------------

		if($messages != FALSE)
		{
			foreach($messages as $idx=>$message)
			{
				//-----------------------------------------------------------------
				//  Figure out if it's new or old
				//-----------------------------------------------------------------

				if($message['r_date'] == "unread") $new_old = "new";
				else $new_old = "old";

				//-----------------------------------------------------------------
				//  See if user exists
				//-----------------------------------------------------------------

				if(isset($message['uid'])) $message['s_user_user'] = "<a href='$PHP_SELF?n=modules/users&a=1&d={$message['s_user_id']}'>{$message['s_user_user']}</a>";
				else $message['s_user_user'] = $message['s_user_user'];

				//-----------------------------------------------------------------
				//  Make Layout
				//-----------------------------------------------------------------

				$layout .= "
					<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."' onclick=window.location.href='$PHP_SELF?n=modules/pm&a=2&d={$message['pid']}&s=$s&t=$t2&p=$p'>
					<td><img src='templates/$template/images/buttons/$new_old.gif' border='0' alt=''></td>
					<td><a href='$PHP_SELF?n=modules/pm&a=2&d={$message['pid']}&s=$s&t=$t2&p=$p'>{$message['subject']}</a></td>
					<td>".today_yesterday($message['s_date'])."</td>
					<td>{$message['s_user_user']}</td>
					<td><a href='$PHP_SELF?n=modules/pm&a=5&d={$message['pid']}&s=$s&t=$t2&p=$p'><img src='templates/$template/images/buttons/delete.gif' border='0' alt='{$language['PM Inbox desc']}'></a></td>
					</tr>";

				//-----------------------------------------------------------------
				//  Start from 1
				//-----------------------------------------------------------------

				$idx++;
			}
		}
		else
		{
			//-----------------------------------------------------------------
			//  Reset to 0
			//-----------------------------------------------------------------

			$idx = 0;

			//-----------------------------------------------------------------
			//  Make Layout
			//-----------------------------------------------------------------

			$layout .= "<tr class='con1'><td colspan='5'>{$language['PM Inbox desc2']}</td></tr>";
		}

		//-----------------------------------------------------------------
		//  Navigation Code
		//-----------------------------------------------------------------

		if($p == 1) $nav_prev = "<tr><td class='con1' align='right'>&nbsp;</td>";
		else
		{
			$prevpage = $p-1;
			$nav_prev = "<tr><td class='con1' align='left'><a href='$PHP_SELF?n=modules/pm&s=$s&t=$t2&p=$prevpage'>&laquo; {$language['PM Nav previous']}</a></td>";
		}

		$nav_nr = '[ '.paginate($lastpage, $p, $PHP_SELF.'?n=modules/pm&s='.$s.'&t='.$t2).' ]';

		if($idx == $numrows) $nav_nr = NULL;

		if($p == $lastpage)
			$nav_next = "<td class='con1' align='right'>&nbsp;</td></tr>";
		else
		{
			$nextcode = $numrows - $idx * $p;
			if($nextcode > $idx) $how_next = $idx;
			else $how_next = $nextcode;

			$nextpage = $p+1;

			$nav_next = "<td class='con1' align='right'><a href='$PHP_SELF?n=modules/pm&s=$s&t=$t2&p=$nextpage'>{$language['PM Nav next']} $how_next &raquo;</a></td></tr>";
		}

		if($idx == $numrows)
		{
			$nav_head = "class='con1'";
			$nav = NULL;
		}
		else
		{
			$nav_head = "class='head'";
			$nav = ''.$nav_prev.''.$nav_next.'';
		}

		//-----------------------------------------------------------------
		//  Start Layout + Navigation
		//-----------------------------------------------------------------

		?>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='head'><td colspan='5'><?= $language['PM Options name'] ?></td></tr>
			<tr class="bottom">
			<td width="50%"><?= $language['PM Options title'] ?></td>
			<td width="50%"><?= $language['PM Options title2'] ?></td>
			</tr>
			<tr class="con1">
			<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm'><a href="<?= $PHP_SELF ?>?n=modules/pm"><?= $language['PM Options desc'] ?></a></td>
			<td width="50%" rowspan="3"><?= view_inbox_statistics(); ?></td>
			</tr>
			<tr class="con1">
			<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm&a=1'><a href="<?= $PHP_SELF ?>?n=modules/pm&a=1"><?= $language['PM Options desc2'] ?></a></td>
			</tr>
			<tr class="con1">
			<td width="50%" onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='<?= $PHP_SELF ?>?n=modules/pm&a=4'><a href="<?= $PHP_SELF ?>?n=modules/pm&a=4"><?= $language['PM Options desc3'] ?></a></td>
			</tr>
			</table>
			<br>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='head'><td colspan='5'><?= $language['PM Inbox name'] ?></td></tr>
			<tr class="bottom">
			<td width="1%" align="center"><a href="<?= $PHP_SELF ?>?n=modules/pm&s=1&t=<?= $t ?>&p=<?= $p ?>"><? if($s == $sname1) echo $t3; else echo "*" ?></a></td>
			<td width="50%"><a href="<?= $PHP_SELF ?>?n=modules/pm&s=2&t=<?= $t ?>&p=<?= $p ?>"><?= $language['PM Inbox title'] ?> <? if($s == $sname2) echo $t3 ?></a></td>
			<td width="30%"><a href="<?= $PHP_SELF ?>?n=modules/pm&s=3&t=<?= $t ?>&p=<?= $p ?>"><?= $language['PM Inbox title2'] ?> <? if($s == $sname3) echo $t3 ?></a></td>
			<td width="18%"><a href="<?= $PHP_SELF ?>?n=modules/pm&s=4&t=<?= $t ?>&p=<?= $p ?>"><?= $language['PM Inbox title3'] ?> <? if($s == $sname4) echo $t3 ?></a></td>
			<td width="1%">&nbsp;</td>
			</tr>
			<?= $layout ?>
			</table>
			<br>
			<table class="main" cellspacing="1" cellpadding="4" width="100%">
			<col width="50%"><col width="50%">
			<tr <?= $nav_head ?>>
			<td colspan="2">
			<table border="0" cellspacing="0" cellpadding="0" width="100%">
			<col width="50%"><col width="50%">
			<tr class="empty">
			<td align="left"><?= $language['PM Nav nav'] ?> <b><?= $idx ?></b> <?= $language['PM Nav nav2'] ?> <b><?= $numrows ?></b></td>
			<td align="right"><?= $nav_nr ?></td>
			</tr>
			</table>
			</td>
			</tr>
			<?= $nav ?>
			</table>
			<br>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr><td class="con1" width="50%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="7%" class="empty"><img src="templates/<?= $template ?>/images/buttons/new.gif" border="0"></td><td width="93%" class="empty"><?= $language['PM Flag desc'] ?></td></tr>
			</table>
			</td><td class="con1" width="50%">
			<table width="100%" cellspacing="0" cellpadding="0">
			<tr><td width="7%" class="empty"><img src="templates/<?= $template ?>/images/buttons/old.gif" border="0"></td><td width="93%" class="empty"><?= $language['PM Flag desc2'] ?></td></tr>
			</table>
			</td></tr>
			</table>
		<?
}

?>